const mongoose = require('mongoose');

const DB_URL = 'mongodb://localhost:27017/planet';
mongoose.connect(DB_URL, {
	useNewUrlParser: true,
	poolSize: 5
});
mongoose.connection.once('connected', function() {
	console.log('mongo connect success');
});
mongoose.connection.on('error', function() {
	console.log('mongo connect failed');
});

const schemas = {
	plan: {
		user: { type: String, require: true, index: true },
		timeStamp: { type: String, require: true, index: true },
		planName: { type: String, require: true },
		executeDay: { type: String, require: true },
		missions: [
			{
				missionName: { type: String, require: true },
				missionDetails: { type: String, require: true },
				startTime: { type: String, require: true },
				endTime: { type: String, require: true },
				finished:{type:Boolean,require:false}
			}
		]
	},
	user: {
		openid: { type: String, require: true, index: true, unique: true },
		userInfo: {
			nickName: { type: String, require: true },
			gender: { type: Number, require: true },
			language: { type: String },
			city: { type: String, require: true },
			country: { type: String, require: true },
			province: { type: String, require: true },
			avatarUrl:{type:String,require:true}
		}
	},
	work:{
		user:{type:String,require:true,index:true},
		timeStamp:{type:String,require:true,index:true},
		workName:{type:String,require:true},
		workDetails:{type:String},
		finished:{type:Boolean}
	}
};

for (let schema in schemas) {
	mongoose.model(schema, new mongoose.Schema(schemas[schema]));
}

module.exports = {
	getModel: function(modelName) {
		return mongoose.model(modelName);
	},
	specialOpenId:'oDSwj0dNFHzlCabsMjVln5YejXb0'
};
